import { Injectable } from '@nestjs/common';
import { Cat } from '../interfaces/cat.interface';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CatsMember } from './cats.entity';
import { Character } from './characteristic.entity';

@Injectable()
export class CatsService {
  constructor(
    @InjectRepository(CatsMember)
    private usersRepository: Repository<CatsMember>,
    @InjectRepository(Character)
    private characterRep: Repository<Character>,
  ) {}
  // private cats: Cat[] = [];
  async create(cat: Cat) {
    const { name, age, breed, like, food } = cat;
    let msg = null;
    (name == '' || name == null) && (msg = 'name不能为空');
    age == null && (msg = 'age不能为空');
    typeof age == 'string' && age == '' && (msg = 'age不能为空');
    typeof age == 'string' && isNaN(Number(age)) && (msg = 'age为number类型');
    (breed == '' || breed == null) && (msg = 'breed不能为空');
    if (msg == null) {
      let catsMember = new CatsMember();
      catsMember.name = name;
      catsMember.age = age;
      catsMember.breed = breed;

      let character = new Character();
      character.like = like;
      character.food = food;

      character.cats = catsMember; // 联接两者
      await this.usersRepository.save(catsMember);
      await this.characterRep.save(character);

      msg = '添加成功';
    }
    return {
      msg,
    };
  }
  async findAll(): Promise<CatsMember[]> {
    let list = await this.usersRepository
      .createQueryBuilder()
      .leftJoinAndSelect('CatsMember.character', 'character')
      .leftJoinAndSelect('CatsMember.master', 'master')
      .orderBy('CatsMember.id', 'ASC')
      .getMany();
    // let arr = await this.usersRepository.find({ relations: ['character'] });
    let result = list.map((item) => {
      if (item.character != null) {
        delete item.character.id;
      }
      return item;
    });
    return result;
  }
  async remove(id) {
    let msg = null;
    let allCats = await this.usersRepository.find();
    let arr = allCats.filter((item) => item['id'] == id);
    if (!arr.length) {
      msg = '删除实体不存在！';
    } else {
      await this.usersRepository.delete(id);
      msg = '删除成功！';
    }
    return {
      msg,
    };
  }
  async update(id, updateObj) {
    let msg = null;
    const { name, age, breed, like, food } = updateObj;
    let catsUpdate = await this.usersRepository.findOne({ id });
    if (catsUpdate == undefined) {
      msg = '修改实体不存在！';
    } else {
      catsUpdate.name = name;
      catsUpdate.age = age;
      catsUpdate.breed = breed;

      let character = new Character();
      like != undefined && (character.like = like);
      food != undefined && (character.food = food);
      if (like != undefined || food != undefined) {
        character.cats = catsUpdate; // 联接两者
      }

      await this.usersRepository.save(catsUpdate);
      if (like != undefined || food != undefined) {
        await this.characterRep.save(character);
      }

      msg = '修改成功！';
    }
    return {
      msg,
    };
  }
}
